package com.cgnpc.scp.hr.service.impl;

import com.cgnpc.scp.common.bean.CreateFileInfo;
import com.cgnpc.scp.common.bean.MyPageResult;
import com.cgnpc.scp.common.service.AppConstantService;
import com.cgnpc.scp.common.utils.MyPageResultUtil;
import com.cgnpc.scp.hr.mybatis.entity.HrProcessRecord;
import com.cgnpc.scp.hr.mybatis.mapper.client.HrProcessRecordMapper;
import com.cgnpc.scp.hr.service.HrProcessRecordService;
import com.cgnpc.scp.run.utils.CommonExportUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service("hrProcessRecordService")
public class HrProcessRecordServiceImpl implements HrProcessRecordService {
    private static final Logger LOGGER = LoggerFactory.getLogger(HrProcessRecordServiceImpl.class);

    @Autowired
    private HrProcessRecordMapper hrProcessRecordMapper;

    @Autowired
    private AppConstantService appConstantService;

    @Autowired
    private SqlSession sqlSession;

    @Override
    public MyPageResult<HrProcessRecord> queryListPage(Map<String, Object> params) {
        MyPageResult<HrProcessRecord> re = null;
        int pageNum = 1;
        int pageSize = 10;
        try {
            pageNum = Integer.parseInt(params.get("pageNum") + "");
            pageSize = Integer.parseInt(params.get("pageSize") + "");
        }catch (Exception e){
            e.printStackTrace();
        }
        Page<HrProcessRecord> queryPage = PageHelper.startPage(pageNum, pageSize);
        hrProcessRecordMapper.listPage(params);
        PageInfo<HrProcessRecord> pageResult = new PageInfo<>(queryPage.getResult());
        re = MyPageResultUtil.createMyPageResult(pageResult);

        return re;
    }

    @Override
    public CreateFileInfo exportExcel(Map<String, Object> params) throws Exception {
        final String logInfo = "[exportExcel_UUID_" + UUID.randomUUID() + "]";
        CreateFileInfo createFileInfo = null;

        try {
            List<String> titleList = Arrays.asList(
                    "流程类型"
                    ,"流程主题","状态","当前环节","当前审批人","发起人"
                    ,"发起日期"
            );
            List<String> fields = Arrays.asList(
                    "processType"
                    ,"folio","status","currentAct","currentDealName","startUserName"
                    ,"startTime"
            );

            CommonExportUtil<HrProcessRecord> util = new CommonExportUtil<>();
            createFileInfo = util.exportExcel(appConstantService, sqlSession, titleList, fields
                    , "com.cgnpc.scp.hr.mybatis.mapper.client.HrProcessRecordMapper"
                    ,"listPageExport", params);
        } catch (Exception e) {
            LOGGER.error("{}error;", logInfo, e);
            throw e;
        }

        return createFileInfo;
    }
}
